home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / site / HTTP / Request / Common.pm
Encoding:
Perl POD Document  |  1999-12-28  |  8.5 KB  |  316 lines

  1. package HTTP::Request::Common;
  2.  
  3. use strict;
  4. use vars qw(@ISA @EXPORT @EXPORT_OK $VERSION);
  5.  
  6. require Exporter;
  7. @ISA=qw(Exporter);
  8.  
  9. @EXPORT=qw(GET HEAD PUT POST);
  10. @EXPORT_OK=qw(cat);
  11.  
  12. require HTTP::Request;
  13. use Carp();
  14.  
  15. $VERSION = sprintf("%d.%02d", q$Revision: 1.3 $ =~ /(\d+)\.(\d+)/);
  16.  
  17. my $CRLF = "\015\012";   # "\r\n" is not portable
  18.  
  19. sub GET  { _simple_req('GET',  @_); }
  20. sub HEAD { _simple_req('HEAD', @_); }
  21. sub PUT  { _simple_req('PUT' , @_); }
  22.  
  23. sub POST
  24. {
  25.     my $url = shift;
  26.     my $req = HTTP::Request->new(POST => $url);
  27.     my $content;
  28.     $content = shift if @_ and ref $_[0];
  29.     my($k, $v);
  30.     while (($k,$v) = splice(@_, 0, 2)) {
  31.     if (lc($k) eq 'content') {
  32.         $content = $v;
  33.     } else {
  34.         $req->push_header($k, $v);
  35.     }
  36.     }
  37.     my $ct = $req->header('Content-Type');
  38.     unless ($ct) {
  39.     $ct = 'application/x-www-form-urlencoded';
  40.     } elsif ($ct eq 'form-data') {
  41.     $ct = 'multipart/form-data';
  42.     }
  43.  
  44.     if (ref $content) {
  45.     if (lc($ct) eq 'multipart/form-data') {    #XXX: boundary="..."
  46.         my $boundary;
  47.         ($content, $boundary) = form_data($content, $boundary);
  48.         $ct = qq(multipart/form-data; boundary="$boundary");
  49.     } else {
  50.         require URI::URL;
  51.         my $url = URI::URL->new('http:');
  52.         $url->query_form(@$content);
  53.         $content = $url->equery;
  54.     }
  55.     }
  56.  
  57.     $req->header('Content-Type' => $ct);  # might be redundant
  58.     if (defined($content)) {
  59.     $req->header('Content-Length' => length($content));
  60.     $req->content($content);
  61.     }
  62.     $req;
  63. }
  64.  
  65.  
  66. sub _simple_req
  67. {
  68.     my($method, $url) = splice(@_, 0, 2);
  69.     my $req = HTTP::Request->new($method => $url);
  70.     my($k, $v);
  71.     while (($k,$v) = splice(@_, 0, 2)) {
  72.     if (lc($k) eq 'content') {
  73.         $req->add_content($v);
  74.     } else {
  75.         $req->push_header($k, $v);
  76.     }
  77.     }
  78.     $req;
  79. }
  80.  
  81.  
  82. sub form_data   # RFC1867
  83. {
  84.     my($data, $boundary) = @_;
  85.     my @parts;
  86.     my($k,$v);
  87.     while (($k,$v) = splice(@$data, 0, 2)) {
  88.     if (ref $v) {
  89.         my $file = shift(@$v);
  90.         my $usename = shift(@$v);
  91.         unless (defined $usename) {
  92.         $usename = $file;
  93.         $usename =~ s,.*/,, if defined($usename);
  94.         }
  95.         my $disp = qq(form-data; name="$k");
  96.         $disp .= qq(; filename="$usename") if $usename;
  97.         my $content = "";
  98.         my $h = HTTP::Headers->new(@$v);
  99.         my $ct = $h->header("Content-Type");
  100.         if ($file) {
  101.         local(*F);
  102.         local($/) = undef; # slurp files
  103.         open(F, $file) or Carp::croak("Can't open file $file: $!");
  104.         $content = <F>;
  105.         close(F);
  106.         unless ($ct) {
  107.             require LWP::MediaTypes;
  108.             $ct = LWP::MediaTypes::guess_media_type($file);
  109.             $h->header("Content-Type" => $ct); # XXX: content-encoding
  110.         }
  111.         }
  112.         if ($h->header("Content-Disposition")) {
  113.         $h->remove_header("Content-Disposition");
  114.         $disp = $h->remove_header("Content-Disposition");
  115.         }
  116.         if ($h->header("Content")) {
  117.         $content = $h->header("Content");
  118.         $h->remove_header("Content");
  119.         }
  120.         push(@parts, "Content-Disposition: $disp$CRLF" .
  121.                          $h->as_string($CRLF) .
  122.                          "$CRLF$content");
  123.     } else {
  124.         push(@parts, qq(Content-Disposition: form-data; name="$k"$CRLF$CRLF$v));
  125.     }
  126.     }
  127.     return "" unless @parts;
  128.     $boundary = boundary() unless $boundary;
  129.  
  130.     my $bno = 1;
  131.   CHECK_BOUNDARY:
  132.     {
  133.     for (@parts) {
  134.         if (index($_, $boundary) >= 0) {
  135.         $boundary = boundary(++$bno);
  136.         redo CHECK_BOUNDARY;
  137.         }
  138.     }
  139.     last;
  140.     }
  141.  
  142.     my $content = "--$boundary$CRLF" .
  143.                   join("$CRLF--$boundary$CRLF", @parts) .
  144.                   "$CRLF--$boundary--$CRLF";
  145.     wantarray ? ($content, $boundary) : $content;
  146. }
  147.  
  148.  
  149. sub boundary
  150. {
  151.     my $size = shift || 1;
  152.     require MIME::Base64;
  153.     MIME::Base64::encode(join("", map chr(rand(256)), 1..$size*3), "");
  154. }
  155.  
  156. 1;
  157.  
  158. __END__
  159.  
  160. =head1 NAME
  161.  
  162. HTTP::Request::Common - Construct common HTTP::Request objects
  163.  
  164. =head1 SYNOPSIS
  165.  
  166.   use HTTP::Request::Common;
  167.   $ua = LWP::UserAgent->new;
  168.   $ua->request(GET 'http://www.sn.no/');
  169.   $ua->request(POST 'http://somewhere/foo', [foo => bar, bar => foo]);
  170.  
  171. =head1 DESCRIPTION
  172.  
  173. This module provide functions that return newly created HTTP::Request
  174. objects.  These functions are usually more convenient than the
  175. standard HTTP::Request constructor for these common requests.  The
  176. following functions are provided.
  177.  
  178. =over 4
  179.  
  180. =item GET $url, [Header => Value,...]
  181.  
  182. The GET() function returns a HTTP::Request object initialized with the
  183. GET method and the specified URL.  Without additional arguments it
  184. is exactly equivalent to the following call
  185.  
  186.   HTTP::Request->new(GET => $url)
  187.  
  188. but is less clutter.  It also reads better when used together with the
  189. LWP::UserAgent->request() method:
  190.  
  191.   my $ua = new LWP::UserAgent;
  192.   my $res = $ua->request(GET 'http://www.sn.no')
  193.   if ($res->is_success) { ...
  194.  
  195. You can also initialize the header values in the request by specifying
  196. some key/value pairs as optional arguments.  For instance:
  197.  
  198.   $ua->request(GET 'http://www.sn.no',
  199.                If_Match => 'foo',
  200.                    From     => 'gisle@aas.no',
  201.               );
  202.  
  203. A header key called 'Content' is special and when seen the value will
  204. initialize the content part of the request instead of setting a header.
  205.  
  206. =item HEAD $url, [Header => Value,...]
  207.  
  208. Like GET() but the method in the request is HEAD.
  209.  
  210. =item PUT $url, [Header => Value,...]
  211.  
  212. Like GET() but the method in the request is PUT.
  213.  
  214. =item POST $url, [$form_ref], [Header => Value,...]
  215.  
  216. This works mostly like GET() with POST as method, but this function
  217. also takes a second optional array reference parameter ($form_ref).
  218. This argument can be used to pass key/value pairs for the form
  219. content.  By default we will initialize a request using the
  220. C<application/x-www-form-urlencoded> content type.  This means that
  221. you can emulate a HTML E<lt>form> POSTing like this:
  222.  
  223.   POST 'http://www.perl.org/survey.cgi',
  224.        [ name  => 'Gisle',
  225.          email => 'gisle@aas.no',
  226.          gender => 'm',
  227.          born   => '1964',
  228.          trust  => '3%',
  229.     ];
  230.  
  231. This will create a HTTP::Request object that looks like this:
  232.  
  233.   POST http://www.perl.org/survey.cgi
  234.   Content-Length: 61
  235.   Content-Type: application/x-www-form-urlencoded
  236.  
  237.   name=Gisle&email=gisle%40aas.no&gender=m&born=1964&trust=3%25
  238.  
  239. The POST method also supports the C<multipart/form-data> content used
  240. for I<Form-based File Upload> as specified in RFC 1867.  You trigger
  241. this content format by specifying a content type of C<'form-data'>.
  242. If one of the values in the $form_ref is an array reference, then it
  243. is treated as a file part specification with the following values:
  244.  
  245.   [ $file, $filename, Header => Value... ]
  246.  
  247. The first value in the array ($file) is the name of a file to open.
  248. This file will be read an its content placed in the request.  The
  249. routine will croak if the file can't be opened.  Use an undef as $file
  250. value if you want to specify the content directly.  The $filename is
  251. the filename to report in the request.  If this value is undefined,
  252. then the basename of the $file will be used.  You can specify an empty
  253. string as $filename if you don't want any filename in the request.
  254.  
  255. Sending my F<~/.profile> to the survey used as example above can be
  256. achieved by this:
  257.  
  258.   POST 'http://www.perl.org/survey.cgi',
  259.        Content_Type => 'form-data',
  260.        Content      => [ name  => 'Gisle Aas',
  261.                          email => 'gisle@aas.no',
  262.                          gender => 'm',
  263.                          born   => '1964',
  264.                          init   => ["$ENV{HOME}/.profile"],
  265.                        ]
  266.  
  267. This will create a HTTP::Request object that almost looks this (the
  268. boundary and the content of your F<~/.profile> is likely to be
  269. different):
  270.  
  271.   POST http://www.perl.org/survey.cgi
  272.   Content-Length: 388
  273.   Content-Type: multipart/form-data; boundary="6G+f"
  274.  
  275.   --6G+f
  276.   Content-Disposition: form-data; name="name"
  277.   
  278.   Gisle Aas
  279.   --6G+f
  280.   Content-Disposition: form-data; name="email"
  281.   
  282.   gisle@aas.no
  283.   --6G+f
  284.   Content-Disposition: form-data; name="gender"
  285.   
  286.   m
  287.   --6G+f
  288.   Content-Disposition: form-data; name="born"
  289.   
  290.   1964
  291.   --6G+f
  292.   Content-Disposition: form-data; name="init"; filename=".profile"
  293.   Content-Type: text/plain
  294.   
  295.   PATH=/local/perl/bin:$PATH
  296.   export PATH
  297.  
  298.   --6G+f--
  299.  
  300. =back
  301.  
  302. =head1 SEE ALSO
  303.  
  304. L<HTTP::Request>, L<LWP::UserAgent>
  305.  
  306.  
  307. =head1 COPYRIGHT
  308.  
  309. Copyright 1997, Gisle Aas
  310.  
  311. This library is free software; you can redistribute it and/or
  312. modify it under the same terms as Perl itself.
  313.  
  314. =cut
  315.  
  316.